﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TranTekBE.Trac_Nghiem
{
    public partial class DanhSachCauHoi : PageRequireLogin
    {
        public Database database = new Database(TinyLib.GetConfigKey("ConnectionString2"));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.MemberKPI != true)
            {
                if (!IsPostBack)
                {

                    //load category

                    var ds = database.ExecuteQuery("SELECT * FROM tbl_DailyCategory");
                    var dt = ds.Tables[0];
                    ddlCategory.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ddlCategory.Items.Add(new ListItem() { Value = dt.Rows[i]["CategoryId"].ToString(), Text = dt.Rows[i]["Name"].ToString() });
                    }
                    LoadData();
                }
            }
            else
            {
                Response.Redirect("../Admin/DontPermission.aspx");
            }

        }
        void LoadData()
        {
            string sql = @"SELECT t2.CategoryId,t2.Name,t1.QuestionId,t1.Content,t1.UpdatedBy,t1.UpdatedDate FROM tbl_DailyQuestion t1 join tbl_DailyCategory t2 on t1.CategoryId=t2.CategoryId WHERE t1.CategoryId={0} AND t1.Content LIKE N'%{1}%'";

            sql = string.Format(sql, ddlCategory.SelectedValue, txtSearch.Text);
            var ds = database.ExecuteQuery(sql);
            LiteralTongSo.Text = ds.Tables[0].Rows.Count.ToString();

            if (ds.Tables.Count > 0)
            {

                var dt = ds.Tables[0];
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        void ClearGridview()
        {
            while (GridView1.Rows.Count > 0)
            {
                GridView1.DeleteRow(0);
            }
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadData();
        }




        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            HiddenField hf = (HiddenField)GridView1.Rows[e.RowIndex].FindControl("QuestionId");
            string questionId = hf.Value;
            string sql = string.Format("DELETE tbl_DailyAnswer WHERE QuestionId={0}; DELETE tbl_DailyQuestion WHERE QuestionId={0}", questionId);
            database.ExecuteQuery(sql);

            int a = e.RowIndex;
            LoadData();
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void txtSearch_TextChanged(object sender, EventArgs e)
        {
            LoadData();

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}